<template>
  <el-dialog :title="!dataForm.areaId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px"
    >
      <el-form-item label="区域名称" prop="areaName">
        <el-input v-model="dataForm.areaName" placeholder="区域名称"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit">确定</el-button>
		</span>
  </el-dialog>
</template>

<script>
import { getTableAreaById, saveOrUpdateTableArea } from '@/api/table/tableArea'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        areaId: 0,
        areaNo: '',
        areaName: '',
        sellerId: '',
        seatNum: '',
        minConsume: '',
        status: '',
        remark: ''
      },
      dataRule: {
        areaNo: [{ required: true, message: '区域编号不能为空', trigger: 'blur' }],
        areaName: [{ required: true, message: '区域名称不能为空', trigger: 'blur' }],
        sellerId: [{ required: true, message: '所属租户不能为空', trigger: 'blur' }],
        seatNum: [{ required: true, message: '座位数不能为空', trigger: 'blur' }],
        minConsume: [{ required: true, message: '最低消费不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '区域状态(1正常2撤销3暂时不能用)不能为空', trigger: 'blur' }],
        remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(areaId) {
      //餐厅类型
      this.dataForm.areaId = areaId || undefined
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.areaId) {
          getTableAreaById(areaId).then(response => {
            let { data } = response
            Object.assign(this.dataForm, data)
          })
        }
      })
    },
    /**
     * 表单提交
     */
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          saveOrUpdateTableArea(this.dataForm).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.visible = false
            this.$emit('refreshDataList')
          })
        }
      })
    }
  }
}
</script>
